package entityUpdater

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commRedisTag"
	"gitee.com/fierce_wolf/go-fox-edge-common/commSql"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeEntity"
)

type DeviceRecordValueUpdater struct {
	recordLastTime map[string]int64
}

func (e *DeviceRecordValueUpdater) updateDeviceRecordValue(deviceName string, manufacturer string, deviceType string, records interface{}) {
	if records == nil {
		return
	}

	recordList := records.([]interface{})

	for _, val := range recordList {
		record := val.(map[string]interface{})

		recordType := record["recordType"]
		if recordType == nil {
			continue
		}

		recordEntity := edgeEntity.Type.DeviceRecordEntity.Instance()
		recordEntity.Manufacturer = manufacturer
		recordEntity.DeviceType = deviceType
		recordEntity.DeviceName = deviceName
		recordEntity.RecordName = recordType.(string)
		recordEntity.RecordData = record

		// 保存到数据库
		err := commSql.InsertEntity(recordEntity)
		if err != nil {
			continue
		}

		// 更新标记
		commRedisTag.Service.SetValue(recordEntity.Type(), recordEntity)
	}

}
